<template>
  <div class="app-container">
    <div>
      <BasicBusiness ref="basicBusinessRef" />
    </div>
    <div style="padding: 50px">
      <Step ref="stepRef" />
    </div>
    <el-form
      ref="elForm"
      :model="formData"
      :rules="rules"
      size="medium"
      label-width="100px"
    >
      <el-form-item label="交付申请表" prop="deliveryApplyFileId" required>
        <el-upload
          ref="deliveryApplyFileId"
          :file-list="deliveryApplyFileIdfileList"
          :action="deliveryApplyFileIdAction"
          :before-upload="deliveryApplyFileIdBeforeUpload"
          :limit="1"
          :on-success="handleUploadSuccess"
          :on-remove="handleRemove"
          :headers="headers"
        >
          <el-button size="small" type="primary" icon="el-icon-upload"
            >点击上传</el-button
          >
        </el-upload>
      </el-form-item>

      <el-form-item label="交付经理" prop="deliveryManager">
        <el-select
          v-model="formData.deliveryManager"
          placeholder="请选择交付经理"
          clearable
          :style="{ width: '100%' }"
        >
          <el-option
            v-for="(item, index) in deliveryManagerOptions"
            :key="index"
            :label="item.userName"
            :value="item.userId"
            :disabled="item.disabled"
          ></el-option>
        </el-select>
      </el-form-item>

      <el-form-item size="mini">
        <el-button type="primary" size="mini" @click="submitForm"
          >提交</el-button
        >
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import BasicBusiness from "@/views/components/business/BasicBusiness.vue";
import Step from "@/views/components/business/Step.vue";
import { getToken } from "@/utils/auth";
import { listUserRoleId } from "@/api/system/user";

import { projectDisclosure, getProjectDetail } from "@/api/project/index";
export default {
  components: {
    BasicBusiness,
    Step,
  },
  data() {
    return {
      formData: {
        deliveryApplyFileId: null,
        deliveryManager: undefined,
      },
      stage: 2,
      rules: {
        signModel: [
          {
            required: true,
            message: "请上传交付申请表",
            trigger: "change",
          },
        ],
        deliveryManager: [
          {
            required: true,
            message: "请选择交付经理",
            trigger: "change",
          },
        ],
      },
      deliveryManagerOptions: [],
      headers: {
        Authorization: "Bearer " + getToken(),
      },
      deliveryApplyFileIdAction:
        "http://business.byteturing.com" +
        process.env.VUE_APP_BASE_API +
        "/common/upload",
      deliveryApplyFileIdfileList: [],
    };
  },
  computed: {
    boId() {
      return this.$route.query.boId;
    },
    projectId() {
      return this.$route.query.projectId;
    },
  },
  mounted() {
    this.loadData();
  },
  methods: {
    loadData() {
      this.$refs.basicBusinessRef.loadData(this.boId).then((res) => {});
      getProjectDetail(this.boId).then((res) => {
        ;
        this.stage = res.data.stage;
        this.$refs.stepRef.setStage(this.stage);
      });
      // 交付经理
      listUserRoleId({ pageNum: 1, pageSize: 9999, roleId: 104 }).then(
        (res) => {
          this.deliveryManagerOptions = res.rows;
        }
      );
    },
    submitForm() {
      this.$refs["elForm"].validate((valid) => {
        if (!valid) return;
        // TODO 提交表单
        // 提交解决方案
        projectDisclosure({
          boId: this.boId,
          // deliveryApplyFileId: this.formData.deliveryApplyFileId,
          ...this.formData
        }).then((res) => {
          this.$message({
            message: "操作成功",
            type: "success",
          });
          this.$router.replace("/implement/index");
        });
      });
    },
    resetForm() {
      this.$refs["elForm"].resetFields();
    },
    deliveryApplyFileIdBeforeUpload(file) {
      let isRightSize = file.size / 1024 / 1024 < 100;
      if (!isRightSize) {
        this.$message.error("文件大小超过 100MB");
      }
      return isRightSize;
    },
    handleUploadSuccess(res, file) {
      this.formData.deliveryApplyFileId = res.fileId;
    },
    handleRemove() {
      this.formData.deliveryApplyFileId = "";
    },
  },
};
</script>

<style lang="scss" scoped></style>
